package com.kamistoat.meimeistore.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.common.valid.BrandAddGroup;
import com.kamistoat.common.valid.BrandSTUpdateGroup;
import com.kamistoat.common.valid.BrandWholeUpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kamistoat.meimeistore.product.entity.BrandEntity;
import com.kamistoat.meimeistore.product.service.BrandService;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;


/**
 * 品牌
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@RestController
@RequestMapping("meimeistoreproduct/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistoreproduct:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("meimeistoreproduct:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistoreproduct:brand:save")
    // 使用BindingResult接收数据校验的错误异常，而如果删除掉BindingResult，异常被直接抛出，可以用@ControllerAdvice来接收。
    public R save(@Validated({BrandAddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/) {
//        if (result.hasErrors()) {
//            // 如果BindingResult中有错误，就获取出这些错误封装成map
//            Map<String, String> errorMap = new HashMap<>();
//            result.getFieldErrors().forEach((fieldError) -> {
//                // 遍历所有错误，拿出每一个错误的字段和错误消息提示，做成键值对放到errorMap中
//                errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
//            });
//            return R.error(400, "提交的数据不合法").put("errorMap", errorMap);
//        } else {
//            brandService.save(brand);
//            return R.ok();
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改原始的update方法，不再直接进行更新
     * 而是要连着具有约束关系的表一起更新
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistoreproduct:brand:update")
    public R update(@Validated({BrandWholeUpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 仅仅修改状态
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("meimeistoreproduct:brand:update")
    public R updateStatus(@Validated({BrandSTUpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistoreproduct:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

}
